package com.example.demo.juc;

import java.util.concurrent.CountDownLatch;

/**
 * ClassName: CountDownLatchDemo
 * author: bob.ly
 * Version: 1.0.0
 * DateTime: 2021/07/31-22:12:00
 * Description:
 */
public class CountDownLatchDemo {
    /**
     * 假设6个同学在教室, 1个值班同学负责锁门
     * <p>
     * 6个线程
     * 1个主线程
     *
     * @param args
     */
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i < 7; i++) {
            new Thread(() -> {
                System.out.println("当前线程:" + Thread.currentThread().getName() + "离开了教室");

                /**
                 * 计算器减一，当前线程不会阻塞
                 */
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }

        /**
         * 当count不为0, 阻塞当前线程, 当countDown为0时, 才调用之后得代码
         */
        countDownLatch.await();
        System.out.println("全部离开了，" + Thread.currentThread().getName() + "线程锁门了");
    }
}
